//login.js
const app = getApp()

Page({
  data: {
    winWidth: 0,
    winHeight: 0,
    path: '', //跳转路径
    dialog2: false
  },
  onLoad: function (options) {
    var that = this;
    /**
     * 获取当前设备的宽高
     */
    var path = options.path
    that.setData({
      path: path,
      winWidth: app.globalData.winWidth,
      winHeight: app.globalData.winHeight
    });
  },
  getUserInfo: function(e) {
    var that = this
    console.log("---------getUserInfo------")
    console.log(e)
    if(e.detail.userInfo){
      console.log("授权展示")
      app.globalData.userInfo = e.detail.userInfo
      // gender 0 未知 1男 2女，nickName 昵称， avatarUrl 头像
      that.setData({
        dialog2: true
      })
    } else {
      console.log("请授权！！！")
    }
    
  },
  cancelLogin: function(){
    wx.switchTab ({
      url: '/pages/index/index'
    })
  },
  closePhoneChoose: function() {
    var that = this
    that.setData({
      dialog2: false
    })
  },
  getPhoneNumber: function(e) {
    console.log("---------getPhoneNumber------")
    console.log(e)
    var that = this
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      wx.checkSession({
        success: function () {
          var data = {
            userInfo: app.globalData.userInfo,
            openId: app.globalData.session.openid,
            shareId: app.globalData.shareId,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            sessionKey: app.globalData.session.session_key
          }
          that.postWechatInfo(data)
        },
        fail: function () {
          var data = {
            userInfo: app.globalData.userInfo,
            openId: app.globalData.session.openid,
            shareId: app.globalData.shareId,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            sessionKey: app.globalData.session.session_key
          }
          that.postWechatInfo(data)
        }
      })
    } else {
      wx.showToast({
        title: '请授权获取手机号码！',
        icon: 'none',
      })
    }
    
    
    
    // wx.reLaunch({
    //   url: '/pages/login/login',
    // })
    // wx.showToast({
    //   title: '获取手机号',
    // });
    // that.setData({
    //   dialog2: false
    // })
  },
  postWechatInfo: function(data) { //获取手机号并且注册 登录
    app.http.post('/wechat/getPhoneNumber', data).then(res =>{
      if(res.status == '200') {
        console.log("wx getPhoneNumber succ!!!")
        // 后台判断进行跳转 邮件信息录入 还是授权码录入，或者首页
        var result = res.data
        wx.setStorageSync('wxUserId', result.wxUserId)
        app.globalData.wxUserId = result.wxUserId
        app.globalData.wxUserType = result.wxUserType
        let path = this.data.path
        if(path != "" && path != undefined){
          if(path == 'vip'){ // ios不直接跳入会员界面
            if(app.globalData.platform == "ios" && !app.globalData.iosPayStatus){
              wx.switchTab ({
                url: '/pages/index/index'
              })
            }
          } else {
            let url = "/pages/" + path + "/" + path
            wx.reLaunch({
              url: url,
            })
          }
        } else {
          if(result.type == '1') { //录入邮件信息
            wx.reLaunch({
              url: '/pages/login/login',
            })
          } else if(result.type == '2') { //授权码
            wx.reLaunch ({
              url: '/pages/authorization/authorization'
            })
          } else if(result.type == '3') { //首页
            wx.switchTab ({
              url: '/pages/index/index'
            })
          }
        }
      } else {
        wx.showModal({
          title: '抱歉',
          content: res.msg,
        })
      }
    })
  }
})
